/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'one_hundred',
                condition: {status:'all'},
                model: {
                    url:_global.url.api + 'one_hundred',
                    event: {
                        '.item-agree:click': 'agree',
                        '.item-reject:click': 'reject'
                    },
                    agree:function(e){
                        e.preventDefault();
                        var id =this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                if(new Model({
                                        url: _global.url.api + 'one_hundred',
                                        data:{id:id,status:1}
                                    }).put()) {
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    reject:function(e){
                        e.preventDefault();
                        var id =this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                if(new Model({
                                        url: _global.url.api + 'one_hundred',
                                        data:{id:id,status:2}
                                    }).put()) {
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存

            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('[name="keyword"]').val();
                _this.collection.reload();
            });
            //上传图片

        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});